home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zvmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.9 KB  |  401 lines

  1. /* Copyright (C) 1989, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zvmem.c,v 1.2 2000/09/19 19:00:55 lpd Exp $ */
  20. /* "Virtual memory" operators */
  21. #include "ghost.h"
  22. #include "gsstruct.h"
  23. #include "oper.h"
  24. #include "estack.h"        /* for checking in restore */
  25. #include "ialloc.h"
  26. #include "idict.h"        /* ditto */
  27. #include "igstate.h"
  28. #include "isave.h"
  29. #include "dstack.h"
  30. #include "stream.h"        /* for files.h */
  31. #include "files.h"        /* for e-stack processing */
  32. #include "store.h"
  33. #include "gsmalloc.h"        /* for gs_memory_default */
  34. #include "gsmatrix.h"        /* for gsstate.h */
  35. #include "gsstate.h"
  36.  
  37. /* Define whether we validate memory before/after save/restore. */
  38. /* Note that we only actually do this if DEBUG is set and -Z? is selected. */
  39. private bool I_VALIDATE_BEFORE_SAVE = true;
  40. private bool I_VALIDATE_AFTER_SAVE = true;
  41. private bool I_VALIDATE_BEFORE_RESTORE = true;
  42. private bool I_VALIDATE_AFTER_RESTORE = true;
  43.  
  44. /* 'Save' structure */
  45. typedef struct vm_save_s vm_save_t;
  46. struct vm_save_s {
  47.     gs_state *gsave;        /* old graphics state */
  48. };
  49.  
  50. gs_private_st_ptrs1(st_vm_save, vm_save_t, "savetype",
  51.             vm_save_enum_ptrs, vm_save_reloc_ptrs, gsave);
  52.  
  53. /* Clean up the stacks and validate storage. */
  54. private void
  55. ivalidate_clean_spaces(i_ctx_t *i_ctx_p)
  56. {
  57.     if (gs_debug_c('?')) {
  58.     ref_stack_cleanup(&d_stack);
  59.     ref_stack_cleanup(&e_stack);
  60.     ref_stack_cleanup(&o_stack);
  61.     ivalidate_spaces();
  62.     }
  63. }
  64.  
  65. /* - save <save> */
  66. int
  67. zsave(i_ctx_t *i_ctx_p)
  68. {
  69.     os_ptr op = osp;
  70.     uint space = icurrent_space;
  71.     vm_save_t *vmsave;
  72.     ulong sid;
  73.     int code;
  74.     gs_state *prev;
  75.  
  76.     if (I_VALIDATE_BEFORE_SAVE)
  77.     ivalidate_clean_spaces(i_ctx_p);
  78.     ialloc_set_space(idmemory, avm_local);
  79.     vmsave = ialloc_struct(vm_save_t, &st_vm_save, "zsave");
  80.     ialloc_set_space(idmemory, space);
  81.     if (vmsave == 0)
  82.     return_error(e_VMerror);
  83.     sid = alloc_save_state(idmemory, vmsave);
  84.     if (sid == 0) {
  85.     ifree_object(vmsave, "zsave");
  86.     return_error(e_VMerror);
  87.     }
  88.     if_debug2('u', "[u]vmsave 0x%lx, id = %lu\n",
  89.           (ulong) vmsave, (ulong) sid);
  90.     code = gs_gsave_for_save(igs, &prev);
  91.     if (code < 0)
  92.     return code;
  93.     code = gs_gsave(igs);
  94.     if (code < 0)
  95.     return code;
  96.     vmsave->gsave = prev;
  97.     push(1);
  98.     make_tav(op, t_save, 0, saveid, sid);
  99.     if (I_VALIDATE_AFTER_SAVE)
  100.     ivalidate_clean_spaces(i_ctx_p);
  101.     return 0;
  102. }
  103.  
  104. /* <save> restore - */
  105. private int restore_check_operand(P3(os_ptr, alloc_save_t **, gs_dual_memory_t *));
  106. private int restore_check_stack(P3(const ref_stack_t *, const alloc_save_t *, bool));
  107. private void restore_fix_stack(P3(ref_stack_t *, const alloc_save_t *, bool));
  108. int
  109. zrestore(i_ctx_t *i_ctx_p)
  110. {
  111.     os_ptr op = osp;
  112.     alloc_save_t *asave;
  113.     bool last;
  114.     vm_save_t *vmsave;
  115.     int code = restore_check_operand(op, &asave, idmemory);
  116.  
  117.     if (code < 0)
  118.     return code;
  119.     if_debug2('u', "[u]vmrestore 0x%lx, id = %lu\n",
  120.           (ulong) alloc_save_client_data(asave),
  121.           (ulong) op->value.saveid);
  122.     if (I_VALIDATE_BEFORE_RESTORE)
  123.     ivalidate_clean_spaces(i_ctx_p);
  124.     /* Check the contents of the stacks. */
  125.     osp--;
  126.     {
  127.     int code;
  128.  
  129.     if ((code = restore_check_stack(&o_stack, asave, false)) < 0 ||
  130.         (code = restore_check_stack(&e_stack, asave, true)) < 0 ||
  131.         (code = restore_check_stack(&d_stack, asave, false)) < 0
  132.         ) {
  133.         osp++;
  134.         return code;
  135.     }
  136.     }
  137.     /* Reset l_new in all stack entries if the new save level is zero. */
  138.     /* Also do some special fixing on the e-stack. */
  139.     restore_fix_stack(&o_stack, asave, false);
  140.     restore_fix_stack(&e_stack, asave, true);
  141.     restore_fix_stack(&d_stack, asave, false);
  142.     /* Iteratively restore the state of memory, */
  143.     /* also doing a grestoreall at each step. */
  144.     do {
  145.     vmsave = alloc_save_client_data(alloc_save_current(idmemory));
  146.     /* Restore the graphics state. */
  147.     gs_grestoreall_for_restore(igs, vmsave->gsave);
  148.     /*
  149.      * If alloc_save_space decided to do a second save, the vmsave
  150.      * object was allocated one save level less deep than the
  151.      * current level, so ifree_object won't actually free it;
  152.      * however, it points to a gsave object that definitely
  153.      * *has* been freed.  In order not to trip up the garbage
  154.      * collector, we clear the gsave pointer now.
  155.      */
  156.     vmsave->gsave = 0;
  157.     /* Now it's safe to restore the state of memory. */
  158.     last = alloc_restore_state_step(asave);
  159.     }
  160.     while (!last);
  161.     {
  162.     uint space = icurrent_space;
  163.  
  164.     ialloc_set_space(idmemory, avm_local);
  165.     ifree_object(vmsave, "zrestore");
  166.     ialloc_set_space(idmemory, space);
  167.     }
  168.     dict_set_top();        /* reload dict stack cache */
  169.     if (I_VALIDATE_AFTER_RESTORE)
  170.     ivalidate_clean_spaces(i_ctx_p);
  171.     return 0;
  172. }
  173. /* Check the operand of a restore. */
  174. private int
  175. restore_check_operand(os_ptr op, alloc_save_t ** pasave,
  176.               gs_dual_memory_t *idmem)
  177. {
  178.     vm_save_t *vmsave;
  179.     ulong sid;
  180.     alloc_save_t *asave;
  181.  
  182.     check_type(*op, t_save);
  183.     vmsave = r_ptr(op, vm_save_t);
  184.     if (vmsave == 0)        /* invalidated save */
  185.     return_error(e_invalidrestore);
  186.     sid = op->value.saveid;
  187.     asave = alloc_find_save(idmem, sid);
  188.     if (asave == 0)
  189.     return_error(e_invalidrestore);
  190.     *pasave = asave;
  191.     return 0;
  192. }
  193. /* Check a stack to make sure all its elements are older than a save. */
  194. private int
  195. restore_check_stack(const ref_stack_t * pstack, const alloc_save_t * asave,
  196.             bool is_estack)
  197. {
  198.     ref_stack_enum_t rsenum;
  199.  
  200.     ref_stack_enum_begin(&rsenum, pstack);
  201.     do {
  202.     const ref *stkp = rsenum.ptr;
  203.     uint size = rsenum.size;
  204.  
  205.     for (; size; stkp++, size--) {
  206.         const void *ptr;
  207.  
  208.         switch (r_type(stkp)) {
  209.         case t_array:
  210.             ptr = stkp->value.refs;
  211.             break;
  212.         case t_dictionary:
  213.             ptr = stkp->value.pdict;
  214.             break;
  215.         case t_file:
  216.             /* Don't check executable or closed literal */
  217.             /* files on the e-stack. */
  218.             {
  219.             stream *s;
  220.  
  221.             if (is_estack &&
  222.                 (r_has_attr(stkp, a_executable) ||
  223.                  file_is_invalid(s, stkp))
  224.                 )
  225.                 continue;
  226.             }
  227.             ptr = stkp->value.pfile;
  228.             break;
  229.         case t_name:
  230.             /* Names are special because of how they are allocated. */
  231.             if (alloc_name_is_since_save(stkp, asave))
  232.             return_error(e_invalidrestore);
  233.             continue;
  234.         case t_string:
  235.             /* Don't check empty executable strings */
  236.             /* on the e-stack. */
  237.             if (r_size(stkp) == 0 &&
  238.             r_has_attr(stkp, a_executable) && is_estack
  239.             )
  240.             continue;
  241.             ptr = stkp->value.bytes;
  242.             break;
  243.         case t_mixedarray:
  244.         case t_shortarray:
  245.             ptr = stkp->value.packed;
  246.             break;
  247.         case t_device:
  248.             ptr = stkp->value.pdevice;
  249.             break;
  250.         case t_fontID:
  251.         case t_struct:
  252.         case t_astruct:
  253.             ptr = stkp->value.pstruct;
  254.             break;
  255.         default:
  256.             continue;
  257.         }
  258.         if (alloc_is_since_save(ptr, asave))
  259.         return_error(e_invalidrestore);
  260.     }
  261.     } while (ref_stack_enum_next(&rsenum));
  262.     return 0;        /* OK */
  263. }
  264. /*
  265.  * If the new save level is zero, fix up the contents of a stack
  266.  * by clearing the l_new bit in all the entries (since we can't tolerate
  267.  * values with l_new set if the save level is zero).
  268.  * Also, in any case, fix up the e-stack by replacing empty executable
  269.  * strings and closed executable files that are newer than the save
  270.  * with canonical ones that aren't.
  271.  *
  272.  * Note that this procedure is only called if restore_check_stack succeeded.
  273.  */
  274. private void
  275. restore_fix_stack(ref_stack_t * pstack, const alloc_save_t * asave,
  276.           bool is_estack)
  277. {
  278.     ref_stack_enum_t rsenum;
  279.  
  280.     ref_stack_enum_begin(&rsenum, pstack);
  281.     do {
  282.     ref *stkp = rsenum.ptr;
  283.     uint size = rsenum.size;
  284.  
  285.     for (; size; stkp++, size--) {
  286.         r_clear_attrs(stkp, l_new);        /* always do it, no harm */
  287.         if (is_estack) {
  288.         ref ofile;
  289.  
  290.         ref_assign(&ofile, stkp);
  291.         switch (r_type(stkp)) {
  292.             case t_string:
  293.             if (r_size(stkp) == 0 &&
  294.                 alloc_is_since_save(stkp->value.bytes,
  295.                         asave)
  296.                 ) {
  297.                 make_empty_const_string(stkp,
  298.                             avm_foreign);
  299.                 break;
  300.             }
  301.             continue;
  302.             case t_file:
  303.             if (alloc_is_since_save(stkp->value.pfile,
  304.                         asave)
  305.                 ) {
  306.                 make_invalid_file(stkp);
  307.                 break;
  308.             }
  309.             continue;
  310.             default:
  311.             continue;
  312.         }
  313.         r_copy_attrs(stkp, a_all | a_executable,
  314.                  &ofile);
  315.         }
  316.     }
  317.     } while (ref_stack_enum_next(&rsenum));
  318. }
  319.  
  320. /* - vmstatus <save_level> <vm_used> <vm_maximum> */
  321. private int
  322. zvmstatus(i_ctx_t *i_ctx_p)
  323. {
  324.     os_ptr op = osp;
  325.     gs_memory_status_t mstat, dstat;
  326.  
  327.     gs_memory_status(imemory, &mstat);
  328.     if (imemory == imemory_global) {
  329.     gs_memory_status_t sstat;
  330.  
  331.     gs_memory_status(imemory_system, &sstat);
  332.     mstat.allocated += sstat.allocated;
  333.     mstat.used += sstat.used;
  334.     }
  335.     gs_memory_status(&gs_memory_default, &dstat);
  336.     push(3);
  337.     make_int(op - 2, imemory_save_level(iimemory_local));
  338.     make_int(op - 1, mstat.used);
  339.     make_int(op, mstat.allocated + dstat.allocated - dstat.used);
  340.     return 0;
  341. }
  342.  
  343. /* ------ Non-standard extensions ------ */
  344.  
  345. /* <save> .forgetsave - */
  346. private int
  347. zforgetsave(i_ctx_t *i_ctx_p)
  348. {
  349.     os_ptr op = osp;
  350.     alloc_save_t *asave;
  351.     vm_save_t *vmsave;
  352.     int code = restore_check_operand(op, &asave, idmemory);
  353.  
  354.     if (code < 0)
  355.     return 0;
  356.     vmsave = alloc_save_client_data(asave);
  357.     /* Reset l_new in all stack entries if the new save level is zero. */
  358.     restore_fix_stack(&o_stack, asave, false);
  359.     restore_fix_stack(&e_stack, asave, false);
  360.     restore_fix_stack(&d_stack, asave, false);
  361.     /*
  362.      * Forget the gsaves, by deleting the bottom gstate on
  363.      * the current stack and the top one on the saved stack and then
  364.      * concatenating the stacks together.
  365.      */
  366.     {
  367.     gs_state *pgs = igs;
  368.     gs_state *last;
  369.  
  370.     while (gs_state_saved(last = gs_state_saved(pgs)) != 0)
  371.         pgs = last;
  372.     gs_state_swap_saved(last, vmsave->gsave);
  373.     gs_grestore(last);
  374.     gs_grestore(last);
  375.     }
  376.     /* Forget the save in the memory manager. */
  377.     alloc_forget_save(asave);
  378.     {
  379.     uint space = icurrent_space;
  380.  
  381.     ialloc_set_space(idmemory, avm_local);
  382.     /* See above for why we clear the gsave pointer here. */
  383.     vmsave->gsave = 0;
  384.     ifree_object(vmsave, "zrestore");
  385.     ialloc_set_space(idmemory, space);
  386.     }
  387.     pop(1);
  388.     return 0;
  389. }
  390.  
  391. /* ------ Initialization procedure ------ */
  392.  
  393. const op_def zvmem_op_defs[] =
  394. {
  395.     {"1.forgetsave", zforgetsave},
  396.     {"1restore", zrestore},
  397.     {"0save", zsave},
  398.     {"0vmstatus", zvmstatus},
  399.     op_def_end(0)
  400. };
  401.